#region The MIT License
// The MIT License
// 
// Copyright (c) 2009 octalforty studios
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;
using System.IO;
using System.Net;
using System.Text;

using NUnit.Framework;

using octalforty.Componento.ComponentModel.Caching;
using octalforty.Componento.ComponentModel.Installation;
using octalforty.Componento.ComponentModel.Installation.Web;
using octalforty.Componento.Shared.SemanticModel;
using Rhino.Mocks;

namespace octalforty.Componento.Tests.Componento.ComponentModel.Installation
{
    [TestFixture()]
    public class RepositoryBrowserServiceTestFixture
    {
        [Test()]
        public void GetComponentDescriptors()
        {
            MockRepository mockRepository = new MockRepository();

            ICacheManagerService cacheManager = mockRepository.StrictMock<ICacheManagerService>();
            IHttpRequestFactoryService httpRequestFactory = mockRepository.StrictMock<IHttpRequestFactoryService>();
            IHttpRequest httpRequest = mockRepository.StrictMock<IHttpRequest>();
            IHttpResponse httpResponse = mockRepository.StrictMock<IHttpResponse>();

            using(mockRepository.Record())
            {
                Expect.Call(cacheManager.GetCacheEntryInfo("http://repository.componento.net/components.xml")).
                    Return(null);

                Expect.Call(httpRequestFactory.CreateHttpRequest(HttpMethod.Get, new Uri("http://repository.componento.net/components.xml"))).
                    Return(httpRequest);

                Expect.Call(httpRequest.GetResponse()).
                    Return(httpResponse);

                Expect.Call(httpResponse.StatusCode).
                    Return(HttpStatusCode.OK);

                Expect.Call(httpResponse.GetResponseStream()).
                    Return(new MemoryStream(Encoding.ASCII.GetBytes(
@"<components xmlns=""http://schemas.componento.net/1.0/components.xsd"">
    <component name=""NUnit"" version=""2.5.0.9122"" manifest-uri=""http://repository.componento.net/components/nunit/manifest.xml?version=2.5.0.9122""
        project-uri=""http://www.nunit.org"" project-logo-uri=""http://www.nunit.org/favicon.ico"">
        <description><![CDATA[NUnit is a unit-testing framework for all .NET languages.]]></description>
        <clis>
            <cli name=""NETFramework"" version=""1.1"" />
            <cli name=""netcompactframework""  />
        </clis>
        <isas>
            <isa name=""cil"" />
        </isas>
        <licenses>
            <license name=""zlib/libpng"" />
        </licenses>  
    </component>
    <component name=""NHibernate"" manifest-uri=""http://repository.componento.net/components/nhibernate/manifest.xml"">
        <description>NHibernate is a mature, open source object-relational mapper for the .NET framework.</description>
    </component>
</components>")));

                Expect.Call(delegate { cacheManager.Add(null, null); }).IgnoreArguments();

                Expect.Call(delegate { httpResponse.Dispose(); });
            } // using

            RepositoryBrowserService repositoryBrowser = new RepositoryBrowserService();
            repositoryBrowser.CacheManager = cacheManager;
            repositoryBrowser.HttpRequestFactory = httpRequestFactory;

            ComponentDescriptor[] entries =
                repositoryBrowser.GetComponentDescriptors(new Uri("http://repository.componento.net"));

            Assert.AreEqual(2, entries.Length);
            Assert.AreEqual(
                new ComponentDescriptor("NUnit", new Version(2, 5, 0, 9122), 
                    new Uri("http://repository.componento.net/components/nunit/manifest.xml?version=2.5.0.9122"),
                    new Uri("http://www.nunit.org"), new Uri("http://www.nunit.org/favicon.ico"),
                    "NUnit is a unit-testing framework for all .NET languages.", 
                    new CliEnvironment[]
                        {
                            new CliEnvironment(CliType.NetFramework, new Version(1, 1)),
                            new CliEnvironment(CliType.NetCompactFramework, null)
                        }, 
                    new IsaType[] { IsaType.Cil },
                    new string[] { "zlib/libpng" }), 
                entries[0]);
            Assert.AreEqual(
                new ComponentDescriptor("NHibernate", null, 
                    new Uri("http://repository.componento.net/components/nhibernate/manifest.xml"), null, 
                    "NHibernate is a mature, open source object-relational mapper for the .NET framework."), 
                entries[1]);
        }
    }
}
